package com.ruoyi.web.controller.function;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.entity.GlPeople;
import com.ruoyi.system.entity.NucleicAcid;
import com.ruoyi.system.entity.VitalSigns;
import com.ruoyi.system.entity.WarningRule;
import com.ruoyi.system.service.GlPeopleService;
import com.ruoyi.system.service.NucleicAcidService;
import com.ruoyi.system.service.VitalSignsService;
import com.ruoyi.system.service.WarningRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName TestController.java
 * @Description TODO
 * @createTime 2023年12月01日 13:33:00
 */
@Api(tags = "测试接口")
@RestController
@RequestMapping("/test1")
public class TestController1 {


    @Autowired
    private GlPeopleService glPeopleService;


    @Autowired
    private VitalSignsService vitalSignsService;

    @Autowired
    private NucleicAcidService nucleicAcidService;

    @Autowired
    private WarningRuleService warningRuleService;

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    @Anonymous
    public AjaxResult test(Long id) {
        LocalDate now = LocalDate.now();
        LocalDateTime startOfDay = now.atStartOfDay();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedStartOfDay = startOfDay.format(formatter);
        System.out.println(formattedStartOfDay);

        System.out.println("预警定时任务" + formattedStartOfDay);
        //当天测试温度  当天核酸检测
        LambdaQueryWrapper<VitalSigns> vitalSignsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //时间大于formattedStartOfDay
        vitalSignsLambdaQueryWrapper.gt(VitalSigns::getMeasurementDate, formattedStartOfDay);

        List<VitalSigns> vitalSignsList = vitalSignsService.list(vitalSignsLambdaQueryWrapper);
        //通过userId去重
        ArrayList<VitalSigns> vitalSigns = vitalSignsList.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() ->
                                new TreeSet<>(Comparator.comparing(VitalSigns::getUserId))), ArrayList::new));

        ArrayList<Long> longs = new ArrayList<>();
        //通过id查找预警规则
        WarningRule warningRule = warningRuleService.getById(id);

        for (VitalSigns vitalSign : vitalSigns) {
            //体温
            if (Double.parseDouble(vitalSign.getTemperature()) > Double.parseDouble(warningRule.getTemperatureS())
                    || Double.parseDouble(vitalSign.getTemperature()) < Double.parseDouble(warningRule.getTemperatureDownX())
                    && Double.parseDouble(vitalSign.getBloodPressure()) > Double.parseDouble(warningRule.getBloodPressureS())
                    || Double.parseDouble(vitalSign.getBloodPressure()) < Double.parseDouble(warningRule.getBloodPressureX())
                    && vitalSign.getRespiratoryRate() > Integer.parseInt(warningRule.getRespiratoryRateS())
                    || vitalSign.getRespiratoryRate() < Integer.parseInt(warningRule.getRespiratoryRateX())
                    && Integer.parseInt(vitalSign.getOxygenSaturation()) > Integer.parseInt(warningRule.getOxygenSaturationS())
                       || Integer.parseInt(vitalSign.getOxygenSaturation()) < Integer.parseInt(warningRule.getOxygenSaturationX())

            ) {
                longs.add(vitalSign.getUserId());
            }
        }

        ArrayList<Long> userIds = new ArrayList<>();
        LambdaQueryWrapper<NucleicAcid> nucleicAcidLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //时间大于formattedStartOfDay
        nucleicAcidLambdaQueryWrapper.gt(NucleicAcid::getDetectionTime, formattedStartOfDay);
        nucleicAcidLambdaQueryWrapper.eq(NucleicAcid::getDetectionResult, "1");
        List<NucleicAcid> nucleicAcidList = nucleicAcidService.list(nucleicAcidLambdaQueryWrapper);
        for (NucleicAcid nucleicAcid : nucleicAcidList) {
            for (Long aLong : longs) {
                if (nucleicAcid.getUserId().equals(aLong)) {
                    userIds.add(aLong);
                }
            }
        }



        String userName = "";
        for (Long userId : userIds) {
            GlPeople glPeople = glPeopleService.getById(userId);
            if (userName.equals("")){
                userName = glPeople.getUserName();
            }
            userName = userName + "," + glPeople.getUserName();
        }
        return AjaxResult.success(userName);
    }

}
